package com.cxyd.modules.api.controller;

import com.cxyd.common.annotation.LogApiOperation;
import com.cxyd.common.utils.ApiResult;
import com.cxyd.modules.api.dto.AgvStateResponseDTO;
import com.cxyd.modules.api.dto.CancelTaskDTO;
import com.cxyd.modules.api.dto.DvcStateDTO;
import com.cxyd.modules.api.dto.RcsResultHK;
import com.cxyd.modules.wcs.dto.AgvCallbackHkDTO;
import com.cxyd.modules.wcs.dto.PostWmsDTO;
import com.cxyd.modules.wcs.dto.ReceiveTaskDTO;
import com.cxyd.modules.wcs.dto.agvCallback.AgvHkRequest;
import com.cxyd.modules.wcs.dto.agvCallback.AgvHkResponse;
import com.cxyd.modules.wcs.service.AgvCallbackHkService;
import com.cxyd.modules.wcs.service.PostWmsService;
import com.cxyd.modules.wcs.service.ReceiveTaskService;
import com.cxyd.modules.wcs.service.StnMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;

@RestController
@RequestMapping("/api")
public class ApiController {
    @Resource
    private ReceiveTaskService receiveTaskService;
    @Resource
    private StnMappingService stnMappingService;
    @Resource
    private AgvCallbackHkService hkService;

    @GetMapping("ping")
    public ApiResult ping() {
        return new ApiResult().success("当前时间:" + LocalTime.now());
    }

    @PostMapping("receive")
    @LogApiOperation("接收上位任务")
    public ApiResult receiveTask(@RequestBody ReceiveTaskDTO dto) {
        try {
            //1.校验数据格式
            if (dto == null) {
                return new ApiResult().feedBack("无效的请求数据!", "404");
            }
            final String requestPk = dto.getRequestPk();
            final String contNo = dto.getContNo();
            if (requestPk.isEmpty() || contNo.isEmpty()) {
                return new ApiResult().feedBack("[请求ID]或[料箱码]不能为空!", "500");
            }
            //2.校验业务数据
            ApiResult checkResult = receiveTaskService.receiveCheckHandler(dto);
            if (!checkResult.getCode().equals("0")) {
                return checkResult;
            }
            //3.将数据保存到[wcs_receive_task]表中(对校验成功的)
            receiveTaskService.save(dto);
            //4.反馈成功
            return new ApiResult().feedBack(requestPk);
        } catch (Exception ex) {
            assert dto != null;
            return new ApiResult().feedBack(ex.getMessage(), dto.getRequestPk());
        }
    }

    //    接收上位取消任务
    @PostMapping("cancel")
    @LogApiOperation("取消任务")
    public ApiResult cancelTask(@RequestBody CancelTaskDTO dto) {
        //1.校验数据格式
        if (dto == null) {
            return new ApiResult().feedBack("无效的请求数据!", "404");
        }
        return receiveTaskService.cancelCheckHandler(dto);
    }

    //    反馈设备状态
//    站点状态更新
    @PostMapping("update-state")
    @LogApiOperation("机台状态更新")
    public ApiResult updateState(@RequestBody DvcStateDTO dto) {
        //1.校验数据格式
        if (dto == null) {
            return new ApiResult().feedBack("无效的请求数据!", "404");
        }
        return stnMappingService.updateStateByStation(dto);
    }

    //    测试小车状态接收
    @PostMapping("robot-state")
    @LogApiOperation("小车状态")
    public ApiResult robotState(@RequestBody AgvStateResponseDTO dto) {
        //1.校验数据格式
        if (dto == null) {
            return new ApiResult().feedBack("无效的请求数据!", "404");
        }
        return new ApiResult().success("成功!");
    }

    @PostMapping("agvCallback")
    @LogApiOperation("HK任务执行通知")
    public RcsResultHK agvCallbackHK(@RequestBody AgvHkResponse dto) {
        try {
//            记录通知信息
            AgvCallbackHkDTO callbackHkDTO = new AgvCallbackHkDTO();
            callbackHkDTO.setTaskCode(dto.getRobotTaskCode());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            callbackHkDTO.setReqTime(sdf.format(new Date()));
            callbackHkDTO.setCurrentPositionCode(dto.getExtra().getValues().getSlotCode());
            callbackHkDTO.setMapCode(dto.getExtra().getValues().getMapCode());
            callbackHkDTO.setMethod(dto.getExtra().getValues().getMethod());
            callbackHkDTO.setRobotCode(dto.getSingleRobotCode());
            callbackHkDTO.setWbCode(dto.getExtra().getValues().getSlotName());
            hkService.save(callbackHkDTO);
//            调用callbackHandle处理业务
            return hkService.callbackHandle(dto);
        } catch (Exception ex) {
            return new RcsResultHK().error(dto.getRobotTaskCode(), ex.getMessage());
        }
    }

    @PostMapping("agvCallbackReq")
    @LogApiOperation("HK任务取放申请通知")
    public RcsResultHK agvCallbackHK(@RequestBody AgvHkRequest dto) {
        try {
//            记录通知信息
            AgvCallbackHkDTO callbackHkDTO = new AgvCallbackHkDTO();
            callbackHkDTO.setTaskCode(dto.getTaskCode());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            callbackHkDTO.setReqTime(sdf.format(new Date()));
            callbackHkDTO.setCurrentPositionCode(dto.getEqptCode());
            callbackHkDTO.setMethod(dto.getMethod());
            hkService.save(callbackHkDTO);
//            调用callbackHandle处理业务
            return hkService.callbackReqHandle(dto);
        } catch (Exception ex) {
            return new RcsResultHK().error(dto.getTaskCode(), ex.getMessage());
        }
    }

    @Autowired
    private PostWmsService postWmsService;
    @PostMapping("insertTask")
    public RcsResultHK agvCallbackHK(@RequestBody ApiResult dto) {
        final PostWmsDTO record = postWmsService.getPostWms();
        postWmsService.postWmsHandle(record, dto);
        return new RcsResultHK().success("成功!");
    }
}
